package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.databinding.ItemProfilesBinding
import com.bawei.module_home.model.ProfilesModelItem

class ProfilesAdapter: RecyclerView.Adapter<ProfilesAdapter.MerchantViewHolder>() {
    val list=mutableListOf<ProfilesModelItem>()

    interface OnItemClickListener{
        fun clickListener(item: ProfilesModelItem, position: Int)
    }
    var itemClickLister: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener){
        itemClickLister=listener
    }


    fun setData(data:List<ProfilesModelItem>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MerchantViewHolder {
        val binding = ItemProfilesBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return MerchantViewHolder(binding)
    }

    override fun onBindViewHolder(
        holder: MerchantViewHolder,
        position: Int
    ) {
        val binding=DataBindingUtil.getBinding<ItemProfilesBinding>(holder.itemView) ?: return
        binding.merchantName.text=list[position].name
        binding.merchantType.text="行业类型:"+list[position].industryType
        binding.merchantComm.text="行业覆盖:"+list[position].stockName
        binding.merchantAddress.text="企业所属地区:"+list[position].address

        holder.itemView.setOnClickListener{
            itemClickLister?.clickListener(list[position],position)
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MerchantViewHolder(val binding: ItemProfilesBinding) : RecyclerView.ViewHolder(binding.root)
}